home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / i386-stub.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  27KB  |  915 lines

  1. /****************************************************************************
  2.  
  3.         THIS SOFTWARE IS NOT COPYRIGHTED
  4.  
  5.    HP offers the following for use in the public domain.  HP makes no
  6.    warranty with regard to the software or it's performance and the
  7.    user accepts the software "AS IS" with all faults.
  8.  
  9.    HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
  10.    TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  11.    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  12.  
  13. ****************************************************************************/
  14.  
  15. /****************************************************************************
  16.  *  Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
  17.  *
  18.  *  Module name: remcom.c $
  19.  *  Revision: 1.34 $
  20.  *  Date: 91/03/09 12:29:49 $
  21.  *  Contributor:     Lake Stevens Instrument Division$
  22.  *
  23.  *  Description:     low level support for gdb debugger. $
  24.  *
  25.  *  Considerations:  only works on target hardware $
  26.  *
  27.  *  Written by:      Glenn Engel $
  28.  *  ModuleState:     Experimental $
  29.  *
  30.  *  NOTES:           See Below $
  31.  *
  32.  *  Modified for 386 by Jim Kingdon, Cygnus Support.
  33.  *
  34.  *  To enable debugger support, two things need to happen.  One, a
  35.  *  call to set_debug_traps() is necessary in order to allow any breakpoints
  36.  *  or error conditions to be properly intercepted and reported to gdb.
  37.  *  Two, a breakpoint needs to be generated to begin communication.  This
  38.  *  is most easily accomplished by a call to breakpoint().  Breakpoint()
  39.  *  simulates a breakpoint by executing a trap #1.
  40.  *
  41.  *  The external function exceptionHandler() is
  42.  *  used to attach a specific handler to a specific 386 vector number.
  43.  *  It should use the same privilege level it runs at.  It should
  44.  *  install it as an interrupt gate so that interrupts are masked
  45.  *  while the handler runs.
  46.  *  Also, need to assign exceptionHook and oldExceptionHook.
  47.  *
  48.  *  Because gdb will sometimes write to the stack area to execute function
  49.  *  calls, this program cannot rely on using the supervisor stack so it
  50.  *  uses it's own stack area reserved in the int array remcomStack.
  51.  *
  52.  *************
  53.  *
  54.  *    The following gdb commands are supported:
  55.  *
  56.  * command          function                               Return value
  57.  *
  58.  *    g             return the value of the CPU registers  hex data or ENN
  59.  *    G             set the value of the CPU registers     OK or ENN
  60.  *
  61.  *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
  62.  *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
  63.  *
  64.  *    c             Resume at current address              SNN   ( signal NN)
  65.  *    cAA..AA       Continue at address AA..AA             SNN
  66.  *
  67.  *    s             Step one instruction                   SNN
  68.  *    sAA..AA       Step one instruction from AA..AA       SNN
  69.  *
  70.  *    k             kill
  71.  *
  72.  *    ?             What was the last sigval ?             SNN   (signal NN)
  73.  *
  74.  * All commands and responses are sent with a packet which includes a
  75.  * checksum.  A packet consists of
  76.  *
  77.  * $<packet info>#<checksum>.
  78.  *
  79.  * where
  80.  * <packet info> :: <characters representing the command or response>
  81.  * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  82.  *
  83.  * When a packet is received, it is first acknowledged with either '+' or '-'.
  84.  * '+' indicates a successful transfer.  '-' indicates a failed transfer.
  85.  *
  86.  * Example:
  87.  *
  88.  * Host:                  Reply:
  89.  * $m0,10#2a               +$00010203040506070809101112131415#42
  90.  *
  91.  ****************************************************************************/
  92.  
  93. #include <stdio.h>
  94. #include <string.h>
  95.  
  96. /************************************************************************
  97.  *
  98.  * external low-level support routines
  99.  */
  100. typedef void (*ExceptionHook)(int);   /* pointer to function with int parm */
  101. typedef void (*Function)();           /* pointer to a function */
  102.  
  103. extern putDebugChar();   /* write a single character      */
  104. extern getDebugChar();   /* read and return a single char */
  105.  
  106. extern Function exceptionHandler();  /* assign an exception handler */
  107. extern ExceptionHook exceptionHook;  /* hook variable for errors/exceptions */
  108.  
  109. /************************************************************************/
  110. /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
  111. /* at least NUMREGBYTES*2 are needed for register packets */
  112. #define BUFMAX 400
  113.  
  114. static char initialized;  /* boolean flag. != 0 means we've been initialized */
  115.  
  116. int     remote_debug;
  117. /*  debug >  0 prints ill-formed commands in valid packets & checksum errors */
  118.  
  119. void waitabit();
  120.  
  121. static const char hexchars[]="0123456789abcdef";
  122.  
  123. /* Number of bytes of registers.  */
  124. #define NUMREGBYTES 64
  125. enum regnames {EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
  126.            PC /* also known as eip */,
  127.            PS /* also known as eflags */,
  128.            CS, SS, DS, ES, FS, GS};
  129.  
  130. /*
  131.  * these should not be static cuz they can be used outside this module
  132.  */
  133. int registers[NUMREGBYTES/4];
  134.  
  135. #define STACKSIZE 10000
  136. int remcomStack[STACKSIZE/sizeof(int)];
  137. static int* stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1];
  138.  
  139. /*
  140.  * In many cases, the system will want to continue exception processing
  141.  * when a continue command is given.
  142.  * oldExceptionHook is a function to invoke in this case.
  143.  */
  144.  
  145. static ExceptionHook oldExceptionHook;
  146.  
  147. /***************************  ASSEMBLY CODE MACROS *************************/
  148. /*                                        */
  149.  
  150. void return_to_prog(void);
  151.  
  152. /* Restore the program's registers (including the stack pointer, which
  153.    means we get the right stack and don't have to worry about popping our
  154.    return address and any stack frames and so on) and return.  */
  155. asm(".text");
  156. asm(".globl _return_to_prog");
  157. asm("_return_to_prog:");
  158. asm("        movw _registers+44, %ss");
  159. asm("        movl _registers+16, %esp");
  160. asm("        movl _registers+4, %ecx");
  161. asm("        movl _registers+8, %edx");
  162. asm("        movl _registers+12, %ebx");
  163. asm("        movl _registers+20, %ebp");
  164. asm("        movl _registers+24, %esi");
  165. asm("        movl _registers+28, %edi");
  166. asm("        movw _registers+48, %ds");
  167. asm("        movw _registers+52, %es");
  168. asm("        movw _registers+56, %fs");
  169. asm("        movw _registers+60, %gs");
  170. asm("        movl _registers+36, %eax");
  171. asm("        pushl %eax");  /* saved eflags */
  172. asm("        movl _registers+40, %eax");
  173. asm("        pushl %eax");  /* saved cs */
  174. asm("        movl _registers+32, %eax");
  175. asm("        pushl %eax");  /* saved eip */
  176. asm("        movl _registers, %eax");
  177. /* use iret to restore pc and flags together so
  178.    that trace flag works right.  */
  179. asm("        iret");
  180.  
  181. #define BREAKPOINT() asm("   int $3");
  182.  
  183. /* Put the error code here just in case the user cares.  */
  184. int gdb_i386errcode;
  185. /* Likewise, the vector number here (since GDB only gets the signal
  186.    number through the usual means, and that's not very specific).  */
  187. int gdb_i386vector = -1;
  188.  
  189. /* GDB stores segment registers in 32-bit words (that's just the way
  190.    m-i386v.h is written).  So zero the appropriate areas in registers.  */
  191. #define SAVE_REGISTERS1() \
  192.   asm ("movl %eax, _registers");                                         \
  193.   asm ("movl %ecx, _registers+4");                           \
  194.   asm ("movl %edx, _registers+8");                           \
  195.   asm ("movl %ebx, _registers+12");                           \
  196.   asm ("movl %ebp, _registers+20");                           \
  197.   asm ("movl %esi, _registers+24");                           \
  198.   asm ("movl %edi, _registers+28");                           \
  199.   asm ("movw $0, %ax");                                 \
  200.   asm ("movw %ds, _registers+48");                           \
  201.   asm ("movw %ax, _registers+50");                         \
  202.   asm ("movw %es, _registers+52");                           \
  203.   asm ("movw %ax, _registers+54");                         \
  204.   asm ("movw %fs, _registers+56");                           \
  205.   asm ("movw %ax, _registers+58");                         \
  206.   asm ("movw %gs, _registers+60");                           \
  207.   asm ("movw %ax, _registers+62");
  208. #define SAVE_ERRCODE() \
  209.   asm ("popl %ebx");                                  \
  210.   asm ("movl %ebx, _gdb_i386errcode");
  211. #define SAVE_REGISTERS2() \
  212.   asm ("popl %ebx"); /* old eip */                           \
  213.   asm ("movl %ebx, _registers+32");                           \
  214.   asm ("popl %ebx");     /* old cs */                           \
  215.   asm ("movl %ebx, _registers+40");                           \
  216.   asm ("movw %ax, _registers+42");                                           \
  217.   asm ("popl %ebx");     /* old eflags */                       \
  218.   asm ("movl %ebx, _registers+36");                          \
  219.   /* Now that we've done the pops, we can save the stack pointer.");  */   \
  220.   asm ("movw %ss, _registers+44");                         \
  221.   asm ("movw %ax, _registers+46");                                                          \
  222.   asm ("movl %esp, _registers+16");
  223.  
  224. /* See if mem_fault_routine is set, if so just IRET to that address.  */
  225. #define CHECK_FAULT() \
  226.   asm ("cmpl $0, _mem_fault_routine");                       \
  227.   asm ("jne mem_fault");
  228.  
  229. asm ("mem_fault:");
  230. /* OK to clobber temp registers; we're just going to end up in set_mem_err.  */
  231. /* Pop error code from the stack and save it.  */
  232. asm ("     popl %eax");
  233. asm ("     movl %eax, _gdb_i386errcode");
  234.  
  235. asm ("     popl %eax"); /* eip */
  236. /* We don't want to return there, we want to return to the function
  237.    pointed to by mem_fault_routine instead.  */
  238. asm ("     movl _mem_fault_routine, %eax");
  239. asm ("     popl %ecx"); /* cs (low 16 bits; junk in hi 16 bits).  */
  240. asm ("     popl %edx"); /* eflags */
  241.  
  242. /* Remove this stack frame; when we do the iret, we will be going to
  243.    the start of a function, so we want the stack to look just like it
  244.    would after a "call" instruction.  */
  245. asm ("     leave");
  246.  
  247. /* Push the stuff that iret wants.  */
  248. asm ("     pushl %edx"); /* eflags */
  249. asm ("     pushl %ecx"); /* cs */
  250. asm ("     pushl %eax"); /* eip */
  251.  
  252. /* Zero mem_fault_routine.  */
  253. asm ("     movl $0, %eax");
  254. asm ("     movl %eax, _mem_fault_routine");
  255.  
  256. asm ("iret");
  257.  
  258. #define CALL_HOOK() asm("call _remcomHandler");
  259.  
  260. /* This function is called when a i386 exception occurs.  It saves
  261.  * all the cpu regs in the _registers array, munges the stack a bit,
  262.  * and invokes an exception handler (remcom_handler).
  263.  *
  264.  * stack on entry:                       stack on exit:
  265.  *   old eflags                          vector number
  266.  *   old cs (zero-filled to 32 bits)
  267.  *   old eip
  268.  *
  269.  */
  270. extern void _catchException3();
  271. asm(".text");
  272. asm(".globl __catchException3");
  273. asm("__catchException3:");
  274. SAVE_REGISTERS1();
  275. SAVE_REGISTERS2();
  276. asm ("pushl $3");
  277. CALL_HOOK();
  278.  
  279. /* Same thing for exception 1.  */
  280. extern void _catchException1();
  281. asm(".text");
  282. asm(".globl __catchException1");
  283. asm("__catchException1:");
  284. SAVE_REGISTERS1();
  285. SAVE_REGISTERS2();
  286. asm ("pushl $1");
  287. CALL_HOOK();
  288.  
  289. /* Same thing for exception 0.  */
  290. extern void _catchException0();
  291. asm(".text");
  292. asm(".globl __catchException0");
  293. asm("__catchException0:");
  294. SAVE_REGISTERS1();
  295. SAVE_REGISTERS2();
  296. asm ("pushl $0");
  297. CALL_HOOK();
  298.  
  299. /* Same thing for exception 4.  */
  300. extern void _catchException4();
  301. asm(".text");
  302. asm(".globl __catchException4");
  303. asm("__catchException4:");
  304. SAVE_REGISTERS1();
  305. SAVE_REGISTERS2();
  306. asm ("pushl $4");
  307. CALL_HOOK();
  308.  
  309. /* Same thing for exception 5.  */
  310. extern void _catchException5();
  311. asm(".text");
  312. asm(".globl __catchException5");
  313. asm("__catchException5:");
  314. SAVE_REGISTERS1();
  315. SAVE_REGISTERS2();
  316. asm ("pushl $5");
  317. CALL_HOOK();
  318.  
  319. /* Same thing for exception 6.  */
  320. extern void _catchException6();
  321. asm(".text");
  322. asm(".globl __catchException6");
  323. asm("__catchException6:");
  324. SAVE_REGISTERS1();
  325. SAVE_REGISTERS2();
  326. asm ("pushl $6");
  327. CALL_HOOK();
  328.  
  329. /* Same thing for exception 7.  */
  330. extern void _catchException7();
  331. asm(".text");
  332. asm(".globl __catchException7");
  333. asm("__catchException7:");
  334. SAVE_REGISTERS1();
  335. SAVE_REGISTERS2();
  336. asm ("pushl $7");
  337. CALL_HOOK();
  338.  
  339. /* Same thing for exception 8.  */
  340. extern void _catchException8();
  341. asm(".text");
  342. asm(".globl __catchException8");
  343. asm("__catchException8:");
  344. SAVE_REGISTERS1();
  345. SAVE_ERRCODE();
  346. SAVE_REGISTERS2();
  347. asm ("pushl $8");
  348. CALL_HOOK();
  349.  
  350. /* Same thing for exception 9.  */
  351. extern void _catchException9();
  352. asm(".text");
  353. asm(".globl __catchException9");
  354. asm("__catchException9:");
  355. SAVE_REGISTERS1();
  356. SAVE_REGISTERS2();
  357. asm ("pushl $9");
  358. CALL_HOOK();
  359.  
  360. /* Same thing for exception 10.  */
  361. extern void _catchException10();
  362. asm(".text");
  363. asm(".globl __catchException10");
  364. asm("__catchException10:");
  365. SAVE_REGISTERS1();
  366. SAVE_ERRCODE();
  367. SAVE_REGISTERS2();
  368. asm ("pushl $10");
  369. CALL_HOOK();
  370.  
  371. /* Same thing for exception 12.  */
  372. extern void _catchException12();
  373. asm(".text");
  374. asm(".globl __catchException12");
  375. asm("__catchException12:");
  376. SAVE_REGISTERS1();
  377. SAVE_ERRCODE();
  378. SAVE_REGISTERS2();
  379. asm ("pushl $12");
  380. CALL_HOOK();
  381.  
  382. /* Same thing for exception 16.  */
  383. extern void _catchException16();
  384. asm(".text");
  385. asm(".globl __catchException16");
  386. asm("__catchException16:");
  387. SAVE_REGISTERS1();
  388. SAVE_REGISTERS2();
  389. asm ("pushl $16");
  390. CALL_HOOK();
  391.  
  392. /* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff.  */
  393.  
  394. /* Same thing for exception 13.  */
  395. extern void _catchException13 ();
  396. asm (".text");
  397. asm (".globl __catchException13");
  398. asm ("__catchException13:");
  399. CHECK_FAULT();
  400. SAVE_REGISTERS1();
  401. SAVE_ERRCODE();
  402. SAVE_REGISTERS2();
  403. asm ("pushl $13");
  404. CALL_HOOK();
  405.  
  406. /* Same thing for exception 11.  */
  407. extern void _catchException11 ();
  408. asm (".text");
  409. asm (".globl __catchException11");
  410. asm ("__catchException11:");
  411. CHECK_FAULT();
  412. SAVE_REGISTERS1();
  413. SAVE_ERRCODE();
  414. SAVE_REGISTERS2();
  415. asm ("pushl $11");
  416. CALL_HOOK();
  417.  
  418. /* Same thing for exception 14.  */
  419. extern void _catchException14 ();
  420. asm (".text");
  421. asm (".globl __catchException14");
  422. asm ("__catchException14:");
  423. CHECK_FAULT();
  424. SAVE_REGISTERS1();
  425. SAVE_ERRCODE();
  426. SAVE_REGISTERS2();
  427. asm ("pushl $14");
  428. CALL_HOOK();
  429.  
  430. /*
  431.  * remcomHandler is a front end for handle_exception.  It moves the
  432.  * stack pointer into an area reserved for debugger use.
  433.  */
  434. asm("_remcomHandler:");
  435. asm("           popl %eax");        /* pop off return address     */
  436. asm("           popl %eax");      /* get the exception number   */
  437. asm("        movl _stackPtr, %esp"); /* move to remcom stack area  */
  438. asm("        pushl %eax");    /* push exception onto stack  */
  439. asm("        call  _handle_exception");    /* this never returns */
  440.  
  441. void _returnFromException()
  442. {
  443.   return_to_prog ();
  444. }
  445.  
  446. int hex(ch)
  447. char ch;
  448. {
  449.   if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
  450.   if ((ch >= '0') && (ch <= '9')) return (ch-'0');
  451.   if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
  452.   return (-1);
  453. }
  454.  
  455.  
  456. /* scan for the sequence $<data>#<checksum>     */
  457. void getpacket(buffer)
  458. char * buffer;
  459. {
  460.   unsigned char checksum;
  461.   unsigned char xmitcsum;
  462.   int  i;
  463.   int  count;
  464.   char ch;
  465.  
  466.   do {
  467.     /* wait around for the start character, ignore all other characters */
  468.     while ((ch = getDebugChar()) != '$');
  469.     checksum = 0;
  470.     xmitcsum = -1;
  471.  
  472.     count = 0;
  473.  
  474.     /* now, read until a # or end of buffer is found */
  475.     while (count < BUFMAX) {
  476.       ch = getDebugChar();
  477.       if (ch == '#') break;
  478.       checksum = checksum + ch;
  479.       buffer[count] = ch;
  480.       count = count + 1;
  481.       }
  482.     buffer[count] = 0;
  483.  
  484.     if (ch == '#') {
  485.       xmitcsum = hex(getDebugChar()) << 4;
  486.       xmitcsum += hex(getDebugChar());
  487.       if ((remote_debug ) && (checksum != xmitcsum)) {
  488.         fprintf(stderr,"bad checksum.  My count = 0x%x, sent=0x%x. buf=%s\n",
  489.                              checksum,xmitcsum,buffer);
  490.       }
  491.  
  492. #if 1
  493. /* Humans shouldn't have to figure out checksums to type to it. */
  494.       putDebugChar ('+');
  495.       return;
  496. #endif
  497.       if (checksum != xmitcsum) putDebugChar('-');  /* failed checksum */
  498.       else {
  499.      putDebugChar('+');  /* successful transfer */
  500.      /* if a sequence char is present, reply the sequence ID */
  501.      if (buffer[2] == ':') {
  502.         putDebugChar( buffer[0] );
  503.         putDebugChar( buffer[1] );
  504.         /* remove sequence chars from buffer */
  505.         count = strlen(buffer);
  506.         for (i=3; i <= count; i++) buffer[i-3] = buffer[i];
  507.      }
  508.       }
  509.     }
  510.   } while (checksum != xmitcsum);
  511.  
  512. }
  513.  
  514. /* send the packet in buffer.  */
  515.  
  516.  
  517. void putpacket(buffer)
  518. char * buffer;
  519. {
  520.   unsigned char checksum;
  521.   int  count;
  522.   char ch;
  523.  
  524.   /*  $<packet info>#<checksum>. */
  525.   do {
  526.   putDebugChar('$');
  527.   checksum = 0;
  528.   count    = 0;
  529.  
  530.   while (ch=buffer[count]) {
  531.     if (! putDebugChar(ch)) return;
  532.     checksum += ch;
  533.     count += 1;
  534.   }
  535.  
  536.   putDebugChar('#');
  537.   putDebugChar(hexchars[checksum >> 4]);
  538.   putDebugChar(hexchars[checksum % 16]);
  539.  
  540.   } while (getDebugChar() != '+');
  541.  
  542. }
  543.  
  544. char  remcomInBuffer[BUFMAX];
  545. char  remcomOutBuffer[BUFMAX];
  546. static short error;
  547.  
  548.  
  549. void debug_error(format, parm)
  550. char * format;
  551. char * parm;
  552. {
  553.   if (remote_debug) fprintf(stderr,format,parm);
  554. }
  555.  
  556. /* Address of a routine to RTE to if we get a memory fault.  */
  557. static volatile void (*mem_fault_routine)() = NULL;
  558.  
  559. /* Indicate to caller of mem2hex or hex2mem that there has been an
  560.    error.  */
  561. static volatile int mem_err = 0;
  562.  
  563. void
  564. set_mem_err ()
  565. {
  566.   mem_err = 1;
  567. }
  568.  
  569. /* These are separate functions so that they are so short and sweet
  570.    that the compiler won't save any registers (if there is a fault
  571.    to mem_fault, they won't get restored, so there better not be any
  572.    saved).  */
  573. int
  574. get_char (addr)
  575.      char *addr;
  576. {
  577.   return *addr;
  578. }
  579.  
  580. void
  581. set_char (addr, val)
  582.      char *addr;
  583.      int val;
  584. {
  585.   *addr = val;
  586. }
  587.  
  588. /* convert the memory pointed to by mem into hex, placing result in buf */
  589. /* return a pointer to the last char put in buf (null) */
  590. /* If MAY_FAULT is non-zero, then we should set mem_err in response to
  591.    a fault; if zero treat a fault like any other fault in the stub.  */
  592. char* mem2hex(mem, buf, count, may_fault)
  593. char* mem;
  594. char* buf;
  595. int   count;
  596. int may_fault;
  597. {
  598.       int i;
  599.       unsigned char ch;
  600.  
  601.       if (may_fault)
  602.       mem_fault_routine = set_mem_err;
  603.       for (i=0;i<count;i++) {
  604.           ch = get_char (mem++);
  605.       if (may_fault && mem_err)
  606.         return (buf);
  607.           *buf++ = hexchars[ch >> 4];
  608.           *buf++ = hexchars[ch % 16];
  609.       }
  610.       *buf = 0;
  611.       if (may_fault)
  612.       mem_fault_routine = NULL;
  613.       return(buf);
  614. }
  615.  
  616. /* convert the hex array pointed to by buf into binary to be placed in mem */
  617. /* return a pointer to the character AFTER the last byte written */
  618. char* hex2mem(buf, mem, count, may_fault)
  619. char* buf;
  620. char* mem;
  621. int   count;
  622. int may_fault;
  623. {
  624.       int i;
  625.       unsigned char ch;
  626.  
  627.       if (may_fault)
  628.       mem_fault_routine = set_mem_err;
  629.       for (i=0;i<count;i++) {
  630.           ch = hex(*buf++) << 4;
  631.           ch = ch + hex(*buf++);
  632.           set_char (mem++, ch);
  633.       if (may_fault && mem_err)
  634.         return (mem);
  635.       }
  636.       if (may_fault)
  637.       mem_fault_routine = NULL;
  638.       return(mem);
  639. }
  640.  
  641. /* this function takes the 386 exception vector and attempts to
  642.    translate this number into a unix compatible signal value */
  643. int computeSignal( exceptionVector )
  644. int exceptionVector;
  645. {
  646.   int sigval;
  647.   switch (exceptionVector) {
  648.     case 0 : sigval = 8; break; /* divide by zero */
  649.     case 1 : sigval = 5; break; /* debug exception */
  650.     case 3 : sigval = 5; break; /* breakpoint */
  651.     case 4 : sigval = 16; break; /* into instruction (overflow) */
  652.     case 5 : sigval = 16; break; /* bound instruction */
  653.     case 6 : sigval = 4; break; /* Invalid opcode */
  654.     case 7 : sigval = 8; break; /* coprocessor not available */
  655.     case 8 : sigval = 7; break; /* double fault */
  656.     case 9 : sigval = 11; break; /* coprocessor segment overrun */
  657.     case 10 : sigval = 11; break; /* Invalid TSS */
  658.     case 11 : sigval = 11; break; /* Segment not present */
  659.     case 12 : sigval = 11; break; /* stack exception */
  660.     case 13 : sigval = 11; break; /* general protection */
  661.     case 14 : sigval = 11; break; /* page fault */
  662.     case 16 : sigval = 7; break; /* coprocessor error */
  663.     default:
  664.       sigval = 7;         /* "software generated"*/
  665.   }
  666.   return (sigval);
  667. }
  668.  
  669. /**********************************************/
  670. /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
  671. /* RETURN NUMBER OF CHARS PROCESSED           */
  672. /**********************************************/
  673. int hexToInt(char **ptr, int *intValue)
  674. {
  675.     int numChars = 0;
  676.     int hexValue;
  677.  
  678.     *intValue = 0;
  679.  
  680.     while (**ptr)
  681.     {
  682.         hexValue = hex(**ptr);
  683.         if (hexValue >=0)
  684.         {
  685.             *intValue = (*intValue <<4) | hexValue;
  686.             numChars ++;
  687.         }
  688.         else
  689.             break;
  690.  
  691.         (*ptr)++;
  692.     }
  693.  
  694.     return (numChars);
  695. }
  696.  
  697. /*
  698.  * This function does all command procesing for interfacing to gdb.
  699.  */
  700. void handle_exception(int exceptionVector)
  701. {
  702.   int    sigval;
  703.   int    addr, length;
  704.   char * ptr;
  705.   int    newPC;
  706.  
  707.   gdb_i386vector = exceptionVector;
  708.  
  709.   if (remote_debug) printf("vector=%d, sr=0x%x, pc=0x%x\n",
  710.                 exceptionVector,
  711.                 registers[ PS ],
  712.                 registers[ PC ]);
  713.  
  714.   /* reply to host that an exception has occurred */
  715.   sigval = computeSignal( exceptionVector );
  716.   remcomOutBuffer[0] = 'S';
  717.   remcomOutBuffer[1] =  hexchars[sigval >> 4];
  718.   remcomOutBuffer[2] =  hexchars[sigval % 16];
  719.   remcomOutBuffer[3] = 0;
  720.  
  721.   putpacket(remcomOutBuffer);
  722.  
  723.   while (1==1) {
  724.     error = 0;
  725.     remcomOutBuffer[0] = 0;
  726.     getpacket(remcomInBuffer);
  727.     switch (remcomInBuffer[0]) {
  728.       case '?' :   remcomOutBuffer[0] = 'S';
  729.                    remcomOutBuffer[1] =  hexchars[sigval >> 4];
  730.                    remcomOutBuffer[2] =  hexchars[sigval % 16];
  731.                    remcomOutBuffer[3] = 0;
  732.                  break;
  733.       case 'd' : remote_debug = !(remote_debug);  /* toggle debug flag */
  734.                  break;
  735.       case 'g' : /* return the value of the CPU registers */
  736.                 mem2hex((char*) registers, remcomOutBuffer, NUMREGBYTES, 0);
  737.                 break;
  738.       case 'G' : /* set the value of the CPU registers - return OK */
  739.                 hex2mem(&remcomInBuffer[1], (char*) registers, NUMREGBYTES, 0);
  740.                 strcpy(remcomOutBuffer,"OK");
  741.                 break;
  742.  
  743.       /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
  744.       case 'm' :
  745.             /* TRY TO READ %x,%x.  IF SUCCEED, SET PTR = 0 */
  746.                     ptr = &remcomInBuffer[1];
  747.                     if (hexToInt(&ptr,&addr))
  748.                         if (*(ptr++) == ',')
  749.                             if (hexToInt(&ptr,&length))
  750.                             {
  751.                                 ptr = 0;
  752.                 mem_err = 0;
  753.                                 mem2hex((char*) addr, remcomOutBuffer, length, 1);
  754.                 if (mem_err) {
  755.                     strcpy (remcomOutBuffer, "E03");
  756.                     debug_error ("memory fault");
  757.                 }
  758.                             }
  759.  
  760.                     if (ptr)
  761.                     {
  762.               strcpy(remcomOutBuffer,"E01");
  763.               debug_error("malformed read memory command: %s",remcomInBuffer);
  764.             }
  765.               break;
  766.  
  767.       /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  768.       case 'M' :
  769.             /* TRY TO READ '%x,%x:'.  IF SUCCEED, SET PTR = 0 */
  770.                     ptr = &remcomInBuffer[1];
  771.                     if (hexToInt(&ptr,&addr))
  772.                         if (*(ptr++) == ',')
  773.                             if (hexToInt(&ptr,&length))
  774.                                 if (*(ptr++) == ':')
  775.                                 {
  776.                     mem_err = 0;
  777.                                     hex2mem(ptr, (char*) addr, length, 1);
  778.  
  779.                     if (mem_err) {
  780.                     strcpy (remcomOutBuffer, "E03");
  781.                     debug_error ("memory fault");
  782.                     } else {
  783.                         strcpy(remcomOutBuffer,"OK");
  784.                     }
  785.  
  786.                                     ptr = 0;
  787.                                 }
  788.                     if (ptr)
  789.                     {
  790.               strcpy(remcomOutBuffer,"E02");
  791.               debug_error("malformed write memory command: %s",remcomInBuffer);
  792.             }
  793.                 break;
  794.  
  795.      /* cAA..AA    Continue at address AA..AA(optional) */
  796.      /* sAA..AA   Step one instruction from AA..AA(optional) */
  797.      case 'c' :
  798.      case 's' :
  799.           /* try to read optional parameter, pc unchanged if no parm */
  800.          ptr = &remcomInBuffer[1];
  801.          if (hexToInt(&ptr,&addr))
  802.              registers[ PC ] = addr;
  803.  
  804.           newPC = registers[ PC];
  805.  
  806.           /* clear the trace bit */
  807.           registers[ PS ] &= 0xfffffeff;
  808.  
  809.           /* set the trace bit if we're stepping */
  810.           if (remcomInBuffer[0] == 's') registers[ PS ] |= 0x100;
  811.  
  812.           /*
  813.            * If we found a match for the PC AND we are not returning
  814.            * as a result of a breakpoint (33),
  815.            * trace exception (9), nmi (31), jmp to
  816.            * the old exception handler as if this code never ran.
  817.            */
  818. #if 0
  819.       /* Don't really think we need this, except maybe for protection
  820.          exceptions.  */
  821.                   /*
  822.                    * invoke the previous handler.
  823.                    */
  824.                   if (oldExceptionHook)
  825.                       (*oldExceptionHook) (frame->exceptionVector);
  826.                   newPC = registers[ PC ];    /* pc may have changed  */
  827. #endif /* 0 */
  828.  
  829.       _returnFromException(); /* this is a jump */
  830.  
  831.           break;
  832.  
  833.       /* kill the program */
  834.       case 'k' :  /* do nothing */
  835.         BREAKPOINT();
  836.                 break;
  837.       } /* switch */
  838.  
  839.     /* reply to the request */
  840.     putpacket(remcomOutBuffer);
  841.     }
  842. }
  843.  
  844. /* this function is used to set up exception handlers for tracing and
  845.    breakpoints */
  846. void set_debug_traps()
  847. {
  848. extern void remcomHandler();
  849. int exception;
  850.  
  851.   stackPtr  = &remcomStack[STACKSIZE/sizeof(int) - 1];
  852.  
  853.   exceptionHandler (0, _catchException0);
  854.   exceptionHandler (1, _catchException1);
  855.   exceptionHandler (3, _catchException3);
  856.   exceptionHandler (4, _catchException4);
  857.   exceptionHandler (5, _catchException5);
  858.   exceptionHandler (6, _catchException6);
  859.   exceptionHandler (7, _catchException7);
  860.   exceptionHandler (8, _catchException8);
  861.   exceptionHandler (9, _catchException9);
  862.   exceptionHandler (10, _catchException10);
  863.   exceptionHandler (11, _catchException11);
  864.   exceptionHandler (12, _catchException12);
  865.   exceptionHandler (13, _catchException13);
  866.   exceptionHandler (14, _catchException14);
  867.   exceptionHandler (16, _catchException16);
  868.  
  869.   if (exceptionHook != remcomHandler)
  870.   {
  871.       oldExceptionHook = exceptionHook;
  872.       exceptionHook    = remcomHandler;
  873.   }
  874.  
  875.   /* In case GDB is started before us, ack any packets (presumably
  876.      "$?#xx") sitting there.  */
  877.   putDebugChar ('+');
  878.  
  879.   initialized = 1;
  880.  
  881. }
  882.  
  883. /* This function will generate a breakpoint exception.  It is used at the
  884.    beginning of a program to sync up with a debugger and can be used
  885.    otherwise as a quick means to stop program execution and "break" into
  886.    the debugger. */
  887.  
  888. void breakpoint()
  889. {
  890.   if (initialized)
  891. #if 0
  892.     handle_exception(3);
  893. #else
  894.     BREAKPOINT();
  895. #endif
  896.   waitabit();
  897. }
  898.  
  899. int waitlimit = 1000000;
  900.  
  901. #if 0
  902. void
  903. bogon()
  904. {
  905.   waitabit();
  906. }
  907. #endif
  908.  
  909. void
  910. waitabit()
  911. {
  912.   int i;
  913.   for (i = 0; i < waitlimit; i++) ;
  914. }
  915.